﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace TCS.Web.Admin.AdministrativeManage
{
    public partial class add : System.Web.UI.Page
    {
        private string toadd = "├";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                LoadData();

            }
        }
        void LoadData()
        {
            LoadChildData(0);
        }
        private void LoadChildData(int parentid)
        {
            DataSet ds = new TCS.BLL.Gsp_Administrative().GetListNew(0, "parentid="+parentid.ToString(), "addid");
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count ; i++)
                {
                    this.dropParent.Items.Add(new ListItem(toadd + "" + ds.Tables[0].Rows[i]["AddName"].ToString(), ds.Tables[0].Rows[i]["ADDID"].ToString()));
                    toadd += "─┴";
                    LoadChildData(int.Parse(ds.Tables[0].Rows[i]["ADDID"].ToString()));
                    toadd = toadd.Substring(0, toadd.Length - 2);
                }
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string strErr = "";
           
            if (this.txtAddName.Text == "")
            {
                strErr += "AddName不能为空！\\n";
            }
            
            if (strErr != "")
            {
                LTP.Common.MessageBox.Show(this, strErr);
                return;
            }
           
            string AddName = this.txtAddName.Text;
            string Remark = this.txtRemark.Text;
            int Parentid = int.Parse(dropParent.SelectedValue);
            string Temp = Parentid == 0 ? "0" : GetParentIdList(Parentid);
         

            TCS.BLL.Gsp_Administrative bll = new TCS.BLL.Gsp_Administrative();
            bll.Add(AddName, Remark, Parentid, Temp,Temp.Split(',').Length.ToString());
            if (chkAddContinue.Checked)
            {
                
                Response.Redirect("Add.aspx");
            }
            else
            {
                
                Response.Redirect("index.aspx");
            }

        }
        private string GetParentIdList(int parentid)
        {
            DataSet ds = new TCS.BLL.Gsp_Administrative().GetListNew(1, "addid=" + parentid.ToString(), "addid");
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0].Rows[0]["temp"] + "," + parentid.ToString();
            }
            return "0";
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtAddName.Text = "";
            this.txtRemark.Text = "";
        }
    }
}
